//
//  MLZLogINViewController.m
//  MotherLover
//
//  Created by lanou on 16/1/8.
//  Copyright © 2016年 zj. All rights reserved.
//

#import "MLZLogINViewController.h"
#import "MLZLogInView.h"
#import "MLZRegiserViewController.h"
#import "MLZInfoTableViewCell.h"
#import "HFStretchableTableHeaderView.h"
@interface MLZLogINViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) MLZLogInView *loginView;
@property (nonatomic, strong) UITableView *infoTableView;
@property (nonatomic, strong) HFStretchableTableHeaderView *hendview;
@property (nonatomic, strong)  UIButton *registerButton;
@end

@implementation MLZLogINViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    if([BmobUser getCurrentUser]){
    [self addInfoView];
    
    }else {
    
    [self addViewsssss];
    }
    [self addbutton];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden= YES;
}
-(void)viewWillAppear:(BOOL)animated{
   
    [self loadView];
    [self viewDidLoad];

}
-(void)addbutton{


    //注册或者注销
    
    
    UIButton *registerButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    registerButton.frame = CGRectMake(KW-50, 20, 40, 40);
    
    if([BmobUser getCurrentUser]){
        [registerButton setTitle:@"注销" forState:(UIControlStateNormal)];
    }else {
        [registerButton setTitle:@"注册" forState:(UIControlStateNormal)];}
    [registerButton setTitleColor:[UIColor lightGrayColor] forState:(UIControlStateNormal)];
    [registerButton addTarget:self action:@selector(regiserAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:registerButton];

}
- (void)addInfoView{
    
    
    
    
    _infoTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, KW, KH-64) style:(UITableViewStylePlain)];
    _infoTableView.delegate = self;
    _infoTableView.dataSource = self;
    _infoTableView.rowHeight = 70;
    [self.view addSubview:_infoTableView];
    //背景
    UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200)];
    bgImageView.contentMode = UIViewContentModeScaleAspectFill;
    bgImageView.clipsToBounds = YES;
    bgImageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"hashiqi" ofType:@"png"]];
    
    //背景之上的内容
    UIView *contentView = [[UIView alloc] initWithFrame:bgImageView.bounds];
    contentView.backgroundColor = [UIColor clearColor];
    _hendview= [[HFStretchableTableHeaderView alloc]init];
    [self.hendview stretchHeaderForTableView:self.infoTableView withView:bgImageView  subViews:contentView];
    
    
    
}
#pragma mark - stretchableTable delegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self.hendview scrollViewDidScroll:scrollView];
}

- (void)viewDidLayoutSubviews
{
    [self.hendview resizeView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{


    return 2;

}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *identifier = @"ZJCell";
    MLZInfoTableViewCell *cell  = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[MLZInfoTableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier];
    }

    
    NSArray *array =@[@"姓名",@"个性签名",];
    cell.imageIcon.text = array[indexPath.row];
    return cell;

}
#pragma mark---------下上是登陆之后的界面 下面是登陆界面;
- (void)addViewsssss{
 
    
    
    
    self.loginView = [[MLZLogInView alloc]initWithFrame:CGRectMake(0, 64, KW, KH)];
    [_loginView.loginButt addTarget:self action:@selector(loginButtAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:_loginView];
    
    
    
}
- (void)loginButtAction:(UIButton*)button{
   
[BmobUser loginInbackgroundWithAccount:_loginView.mailTF.text andPassword:_loginView.passwordTF.text block:^(BmobUser *user, NSError *error) {
    
    if(!error){
        NSLog(@"%@",user);
       NSLog(@"登陆成功");
       
         [_loginView.mailTF resignFirstResponder];
          [_loginView.passwordTF resignFirstResponder];
        [self loadView];
        [self viewDidLoad];
        [self viewWillAppear:YES];
    
    }else {
    
    
        [self alertActionWithTitle:@"错误" message:@"用户名或密码错误" other:@"确定"];
    
    
    
    }
    
    
}];

  



}
- (void)regiserAction:(UIButton*)button{

    if(![BmobUser getCurrentUser]){
    
    MLZRegiserViewController *regiser = [[MLZRegiserViewController alloc]init];
    [self.navigationController pushViewController:regiser animated:YES];
    
    }else {
        
        [BmobUser logout];
        [self loadView];
        [self viewDidLoad];
        [self viewWillAppear:YES];
     
    
    
    }
    
    
    
   






}
- (UIAlertController *)alertActionWithTitle:(NSString *)title message:(NSString *)message other:(NSString *)other
{
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:other style:UIAlertActionStyleDefault handler:nil];
    [alertVc addAction:otherAction];
    [self presentViewController:alertVc animated:YES completion:nil];
    return alertVc;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
